
//
//  ExercisesAnswerFeedbackView.m
//  ShowYork
//
//  Created by hxh on 2020/9/12.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSString *content);

#import "ExercisesAnswerFeedbackView.h"

@interface ExercisesAnswerFeedbackView ()<UITextViewDelegate>
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIView *textView;
@property (nonatomic,strong)UITextView *inputView;
@property (nonatomic,strong)UILabel *placeLb;
@property (nonatomic,strong)UIButton *closeBtn;

@property (nonatomic,copy)NSArray *titleArray;

@property (nonatomic,strong)UIButton *lastBtn;

@property (nonatomic,copy)doneBlock done;

@end

@implementation ExercisesAnswerFeedbackView

- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"题干错误",@"答案错误",@"解析有误",@"解析看不懂",@"题目超纲",@"其他"];
    }
    return _titleArray;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithCompleteBlock:(void (^)(NSString * _Nonnull content))completeBlock{
    if (self = [super init]) {
        [self setUI];
        
        [self registNote];
        
        if (completeBlock) {
            self.done = ^(NSString *content) {
                completeBlock(content);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.4);
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(383);
        make.height.mas_equalTo(383);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"请选择反馈类型";
    self.titleLb.textColor = RGBA(50, 50, 50, 1);
    self.titleLb.font = font(18);
    [self.containView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.containView).offset(16);
        make.centerX.equalTo(self.containView);
    }];
  
    CGFloat margin = 16;
    CGFloat padding = 24;
    
    CGFloat width = (kScreenW - 16 * 2 - 24)/2;
    
    CGFloat height = width * 0.2;
    
    __block UIView *lastView = nil;
    
    [self.titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger row = idx/2;
        NSInteger colum = idx%2;
        
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = idx;
        [btn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:RGBA(50, 50, 50, 1) forState:UIControlStateNormal];
        [btn setTitleColor:RGBA(86, 128, 250, 1) forState:UIControlStateSelected];
        btn.titleLabel.font = font(14);
        [self.containView addSubview:btn];
        
//         ;
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.containView).offset(colum * width + colum * padding + margin);
            make.top.equalTo(self.titleLb.mas_bottom).offset(row * height + row * margin + padding);
            make.size.mas_equalTo(CGSizeMake(width, height));
        }];
        btn.layer.borderColor = RGBA(226, 226, 226, 1).CGColor;
        btn.layer.borderWidth = 1;
        btn.layer.cornerRadius = height/2;
        btn.layer.masksToBounds = YES;

        lastView = btn;
    }];

    self.textView = [[UIView alloc]init];
    self.textView.layer.cornerRadius = 13.5;
    self.textView.layer.borderColor = RGBA(226, 226, 226, 1).CGColor;
    self.textView.layer.borderWidth = 1;
    self.textView.layer.masksToBounds = YES;
    [self.containView addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lastView.mas_bottom).offset(margin);
        make.left.mas_equalTo(self.containView).offset(margin);
        make.right.mas_equalTo(self.containView).offset(-margin);
        make.height.mas_equalTo(self.textView.mas_width).multipliedBy(0.2);
    }];
    
    self.inputView = [[UITextView alloc]init];
    self.inputView.textColor = RGBA(50, 50, 50, 1);
    self.inputView.font = font(14);
    self.inputView.delegate = self;
    [self.containView addSubview:self.inputView];
    
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.textView).offset(16);
        make.right.mas_equalTo(self.textView).offset(-16);
        make.top.mas_equalTo(self.textView).offset(1);
        make.bottom.mas_equalTo(self.textView).offset(-1);
    }];
    
    self.placeLb = [[UILabel alloc]init];
    self.placeLb.text = @"请简述反馈理由（必填）";
    self.placeLb.textColor = RGBA(204, 204, 204, 1);
    self.placeLb.font = font(14);
    [self.containView addSubview:self.placeLb];
    [self.placeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView).offset(9);
        make.left.mas_equalTo(self.textView).offset(21);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.confirmBtn.titleLabel.font = font(18);
    self.confirmBtn.layer.cornerRadius = 22;
    self.confirmBtn.layer.masksToBounds = YES;
    self.confirmBtn.backgroundColor = RGBA(234, 234, 245, 1);
    self.confirmBtn.userInteractionEnabled = NO;
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView.mas_bottom).offset(padding);
        make.left.mas_equalTo(self.containView).offset(margin);
        make.right.mas_equalTo(self.containView).offset(-margin);
        make.height.mas_equalTo(44);
    }];
    
    self.closeBtn = [[UIButton alloc]init];
    [self.closeBtn setImage:[UIImage imageNamed:@"common_close_tag"] forState:UIControlStateNormal];
    [self.closeBtn addTarget:self action:@selector(closeMehod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.closeBtn];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLb);
        make.right.mas_equalTo(self.containView).offset(-16);
    }];
    
}

- (void)closeMehod{
    [self.inputView resignFirstResponder];
    [self removeView];
}

- (void)show{
  
    [[UIApplication sharedApplication].keyWindow addSubview:self];
  
    
    [self layoutIfNeeded];

    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)hide{
    [self dismiss];
    [self removeView];
}

- (void)dismiss{
    
    BOOL stauts = [self.inputView resignFirstResponder];
    
    if (!stauts) {
        [self removeView];
    }
}
- (void)removeView{
    
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(383);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }        
    }];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;
}

- (void)clickMethod:(UIButton *)btn{
    btn.selected = YES;
    if (![self.lastBtn isEqual:btn]) {
        self.lastBtn.selected = NO;
        self.lastBtn.backgroundColor = [UIColor whiteColor];
        self.lastBtn.layer.borderColor = RGBA(226, 226, 226, 1).CGColor;
        btn.backgroundColor = RGBA(86, 128, 250, 0.2);
        btn.layer.borderColor = [UIColor whiteColor].CGColor;
        
    }
    self.lastBtn = btn;
    
    self.confirmBtn.userInteractionEnabled = (self.inputView.text.length > 0 && self.lastBtn != nil);
    
    self.confirmBtn.backgroundColor = (self.inputView.text.length > 0 && self.lastBtn != nil)?[RPAppManager themColor]:RGBA(234, 234, 245, 1);
}

- (void)confirmMethod{
    if (self.done) {
        self.done([NSString stringWithFormat:@"%@:%@",self.lastBtn.titleLabel.text,self.inputView.text]);
    }
}

- (void)registNote{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


#pragma maark - 监听键盘时间
- (void)keyboardWillShow:(NSNotification *)notification
{
    CGFloat time = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];
    CGRect keyboardBounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-keyboardBounds.size.height);
    }];
    [UIView animateWithDuration:time animations:^{
        [self layoutIfNeeded];
    }];
    
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    CGFloat time = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
    
    [UIView animateWithDuration:time animations:^{
        [self layoutIfNeeded];
    }];
}

#pragma mark  - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length > 60) {
        textView.text = [textView.text substringToIndex:60];
    }
    
    
    self.placeLb.hidden = textView.text.length > 0;
    
    self.confirmBtn.userInteractionEnabled = ((textView.text.length > 0 && ![textView.text isSpaceString]) && self.lastBtn != nil);

    self.confirmBtn.backgroundColor = ((textView.text.length > 0 && ![textView.text isSpaceString]) && self.lastBtn != nil)?[RPAppManager themColor]:RGBA(234, 234, 245, 1);
}

@end
